/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package racesuitefx;

import DAO.RaceResult;
import DAO.TagDAO;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import racesuitefx.entities.Tag;

/**
 * FXML Controller class
 *
 * @author Wolfgang
 */
public class FXMLTagsController implements Initializable {

    /**
     * Initializes the controller class.
     */
    @FXML
    private TextField tagId;

    @FXML
    private TextField description;

    @FXML
    private TableView<Tag> tagTable;

    private TagDAO tagDao;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        tagDao = new TagDAO();
        // fill Table with Columns base;
        TableColumn tagId = new TableColumn("Tag ID:");
        tagId.setCellValueFactory(new PropertyValueFactory<Tag, String>("tagId"));

        TableColumn password = new TableColumn("Beschreibung:");
        password.setCellValueFactory(new PropertyValueFactory<Tag, String>("password"));
        tagTable.setItems(FXCollections.observableArrayList(tagDao.findAll()));
        tagTable.getColumns().addAll(tagId, password);
        tagTable.autosize();
    }
    
    @FXML
    private void add(){
        Tag t = new Tag();
        t.setTagId(tagId.getText());
        t.setPassword(description.getText());
        tagDao.create(t);
        tagTable.setItems(FXCollections.observableArrayList(tagDao.findAll()));
        tagTable.autosize();
    }

    @FXML
    private void scanTag(){
        System.out.println("Scan Tag");
        this.tagId.setText(Util.CommunicateUtil.readTagId());
    }
}
